package com.financing.wap.service.impl;


import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.financing.wap.common.BizEnums.CommonEnums;
import com.financing.wap.dao.IFpShopUserRelProductDao;
import com.financing.wap.dao.IFpShopUserRelProductDetailDao;
import com.financing.wap.dto.FpShopUserRelProductDTO;
import com.financing.wap.dto.FpShopUserRelProductDetailDTO;
import com.financing.wap.service.IFpShopUserRelProductService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.StringUtil;
/**
 * fp_shop_user_rel_product  
 * 理财师工具用户自有产品表 业务访问层实现类
 * @author huqiyun
 *
 */

@Transactional(rollbackFor=Exception.class)
@Repository("fpShopUserRelProductService")
public class FpShopUserRelProductServiceImpl implements IFpShopUserRelProductService{
	
	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IFpShopUserRelProductDao fpShopUserRelProductDao;
	
	@Autowired
	private IFpShopUserRelProductDetailDao fpShopUserRelProductDetailDao;
	
	
	@Override
	public int insert(FpShopUserRelProductDTO fpShopUserRelProduct) throws SQLException {
		int id = fpShopUserRelProductDao.insert(fpShopUserRelProduct);
		//修改更多详情
		fpShopUserRelProduct.setId(id);
		String moreDetail = fpShopUserRelProduct.getMoreDetail();
		if(!StringUtil.isNullOrEmptyStr(moreDetail)){
			//修改更多详情
			insertDetail(fpShopUserRelProduct);
		}
		return id;
	}
	
	@Override
	public void update(FpShopUserRelProductDTO fpShopUserRelProduct) throws SQLException {
		
		fpShopUserRelProductDao.update(fpShopUserRelProduct);
		String moreDetail = fpShopUserRelProduct.getMoreDetail();
		if(!StringUtil.isNullOrEmptyStr(moreDetail)){
			//修改更多详情
			insertDetail(fpShopUserRelProduct);
		}
	}
	
	/***
	 * 详情 修改和删除
	 * @param fpShopUserRelProduct
	 * @throws SQLException 
	 */
	private void insertDetail(FpShopUserRelProductDTO fpShopUserRelProduct) throws SQLException {
		Integer relProductId = fpShopUserRelProduct.getId();
		//删除原有的详情
		FpShopUserRelProductDetailDTO oldDetail=new FpShopUserRelProductDetailDTO();
		oldDetail.setRelProductId(relProductId);
		oldDetail.setDeleteTag(CommonEnums.DELETE_FLAG_2);
		fpShopUserRelProductDetailDao.update(oldDetail);
		//新增 产品更多详情处理
		JSONArray moreDetailJsonArray=JSONArray.parseArray(fpShopUserRelProduct.getMoreDetail());
		for (int i = 0; i < moreDetailJsonArray.size(); i++) {
			try {
				JSONObject moreDetailJson = moreDetailJsonArray.getJSONObject(i);
				Object relType=moreDetailJson.get("relType");
				if(relType==null){
					continue;
				}
				Object relImage=moreDetailJson.get("relImage");
				Object relContent=moreDetailJson.get("relContent");
				
				FpShopUserRelProductDetailDTO fpShopUserRelProductDetail=new FpShopUserRelProductDetailDTO();
				fpShopUserRelProductDetail.setRelType(relType+"");
				if(relImage!=null){
					fpShopUserRelProductDetail.setRelImage(relImage+"");
				}
				if(relContent!=null){
					fpShopUserRelProductDetail.setRelContent(relContent+"");
				}
				fpShopUserRelProductDetail.setRelProductId(relProductId);
				DTOdateSetUtil.setCreateInfo(fpShopUserRelProductDetail);
				DTOdateSetUtil.setUpdateInfo(fpShopUserRelProductDetail);
				fpShopUserRelProductDetail.setDeleteTag(CommonEnums.DELETE_FLAG_1);
				fpShopUserRelProductDetailDao.insert(fpShopUserRelProductDetail);
			} catch (Exception e) {
				logger.error("insert fpShopUserRelProductDetail",e);
				continue;
			}
		}
	}

	@Override
	public void updateForce(FpShopUserRelProductDTO fpShopUserRelProduct) throws SQLException {
		fpShopUserRelProductDao.updateForce(fpShopUserRelProduct);
	}

	@Override
	public void delete(Integer id) throws SQLException {
		fpShopUserRelProductDao.delete(id);
	}
	
	@Override
	public List<FpShopUserRelProductDTO> queryList(FpShopUserRelProductDTO fpShopUserRelProduct) throws SQLException {
		return fpShopUserRelProductDao.queryList(fpShopUserRelProduct);
	}

	@Override
	public int queryListCount(FpShopUserRelProductDTO fpShopUserRelProduct) throws SQLException {
		return fpShopUserRelProductDao.queryListCount(fpShopUserRelProduct);
	}

	@Override
	public FpShopUserRelProductDTO loadById(Integer id) throws SQLException {
		return fpShopUserRelProductDao.loadById(id);
	}
 	@Override
	public FpShopUserRelProductDTO queryListGetFirst(FpShopUserRelProductDTO fpShopUserRelProduct)
			throws SQLException {
		 List<FpShopUserRelProductDTO> queryList = queryList(fpShopUserRelProduct);
		 if(queryList==null||queryList.size()==0){
			 return new FpShopUserRelProductDTO();
		 }
		 return queryList.get(0);
	}
}
